﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.CongNo
{
    public partial class frmKetChuyen : Form
    {

        #region Khai báo
        public static bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        StatusBar sb;
        bool checkTrangThai = false;
        DateTime date;
        string ma = "";
        string id = "";
        string year = "";
        string month = "";
        Entities.SoDuCongNo[] hienthi;
        Entities.SoDuCongNo[] hienthi1;
        Entities.SoDuCongNo[] temp;
        string makho = "";
        string tenkho = "";
        public frmKetChuyen()
        {
            InitializeComponent();
            date = Entitystatic.date;
            Entities.CaiDatKho kho = Entitystatic.ViewFile();
            if (kho.MaKho.Length == 0)
            {
                MessageBox.Show("Chưa cài đặt kho", Common.MSG.TieuDe);
                return;
            }
            else
            {
                makho = kho.MaKho;
                tenkho = kho.TenKho;
                cbbNam.Text = date.Year.ToString();
                cbbThang.Text = date.Month.ToString();
                year = date.Year.ToString();
                month = date.Month.ToString();
                cbbDoiTuong.SelectedIndex = 0;
                Select();
            }
        }

        public frmKetChuyen(string makho, int month1, int year1, int loaidoituong)
        {
            InitializeComponent();
            date = DateTime.Parse(month1 + "/01/" + year1);
            cbbNam.Text = date.Year.ToString();
            cbbThang.Text = date.Month.ToString();
            year = date.Year.ToString();
            month = date.Month.ToString();
            this.makho = makho;
            cbbNam.Text = date.Year.ToString();
            cbbThang.Text = date.Month.ToString();
            cbbDoiTuong.SelectedIndex = loaidoituong;
            Select();
        }
        #endregion

        #region kết chuyển
        public void KetChuyen()
        {
            this.Enabled = false;
            try
            {
                bool loaidoituong = false;
                string monthhientai = date.Month.ToString();
                string namhientai = date.Year.ToString();
                if (cbbDoiTuong.SelectedIndex == 0)
                {
                    loaidoituong = false;
                }
                else
                    loaidoituong = true;

                if (hienthi != null)
                {
                    string month = hienthi[0].NgayKetChuyen.Month.ToString();
                    string year = hienthi[0].NgayKetChuyen.Year.ToString();
                    if (month == monthhientai && year == namhientai)
                    {
                        int soluong = 0;
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            if (month == hienthi[i].NgayKetChuyen.Month.ToString() && year == hienthi[i].NgayKetChuyen.Year.ToString() && hienthi[i].LoaiDoiTuong == loaidoituong)
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            return;
                        }
                        frmCongNo a = new frmCongNo(namhientai, monthhientai, cbbDoiTuong.SelectedIndex);
                        if (frmCongNo.mangchitiet.Length == 0)
                        {
                            return;
                        }
                        Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet;
                        pt[0].MaKho = makho;
                        if (Common.StaticVariable.InterfaceObject.UpdateCongNoMang(pt) == true)
                        {
                            Select();
                        }
                    }
                    else
                    {
                        string monthkytruoc = "";
                        string yearkytruoc = "";
                        if ((date.Month - 1) == 0)
                        {
                            monthkytruoc = "12";
                            yearkytruoc = (date.Year - 1).ToString();
                        }
                        else
                        {
                            monthkytruoc = (date.Month - 1).ToString();
                            yearkytruoc = date.Year.ToString();
                        }
                        if (month == monthkytruoc && year == yearkytruoc)
                        {

                            frmCongNo a = new frmCongNo(yearkytruoc, monthkytruoc, cbbDoiTuong.SelectedIndex);
                            if (frmCongNo.mangchitiet.Length == 0)
                            {
                                return;
                            }
                            Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet;
                            pt[0].MaKho = makho;
                            if (Common.StaticVariable.InterfaceObject.UpdateCongNoMang(pt) == true)
                            {
                                Select();
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {
                    string month = cbbThang.Text;
                    string year = cbbNam.Text;

                    if (hienthi1 != null)
                    {
                        int soluong = 0;
                        for (int i = 0; i < hienthi1.Length; i++)
                        {
                            if (month == hienthi1[i].NgayKetChuyen.Month.ToString() && year == hienthi1[i].NgayKetChuyen.Year.ToString())
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            return;
                        }
                    }

                    if (month == monthhientai && year == namhientai)
                    {

                        frmCongNo a = new frmCongNo(namhientai, monthhientai, cbbDoiTuong.SelectedIndex);
                        if (frmCongNo.mangchitiet.Length == 0)
                        {
                            return;
                        }
                        Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet;
                        pt[0].MaKho = makho;
                        if (Common.StaticVariable.InterfaceObject.UpdateCongNoMang(pt) == true)
                        {
                            Select();
                        }
                    }
                    else
                    {
                        string monthkytruoc = "";
                        string yearkytruoc = "";
                        if ((date.Month - 1) == 0)
                        {
                            monthkytruoc = "12";
                            yearkytruoc = (date.Year - 1).ToString();
                        }
                        else
                        {
                            monthkytruoc = (date.Month - 1).ToString();
                            yearkytruoc = date.Year.ToString();
                        }
                        if (month == monthkytruoc && year == yearkytruoc)
                        {

                            frmCongNo a = new frmCongNo(yearkytruoc, monthkytruoc, cbbDoiTuong.SelectedIndex);
                            if (frmCongNo.mangchitiet.Length == 0)
                            {
                                return;
                            }
                            Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet;
                            pt[0].MaKho = makho;
                            if (Common.StaticVariable.InterfaceObject.UpdateCongNoMang(pt) == true)
                            {
                                Select();
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
            this.Enabled = true;
        }
        #endregion

        #region Select
        public void Select()
        {
            try
            {
                bool loaiDoiTuong = false;
                if (cbbDoiTuong.SelectedIndex == 0)
                    loaiDoiTuong = false;
                else
                    loaiDoiTuong = true;
                ArrayList array = new ArrayList();
                Entities.SoDuCongNo[] temp = Common.StaticVariable.InterfaceObject.SelectCongNoTheoKho(makho);
                foreach (Entities.SoDuCongNo item in temp)
                {
                    array.Add(item);
                }
                temp = (Entities.SoDuCongNo[])array.ToArray(typeof(Entities.SoDuCongNo));
                if (temp == null)
                {
                    lbTrangThai.Text = "Kỳ này chưa đóng";
                    lbTrangThai.ForeColor = Color.Black;
                    btnKetChuyen.Enabled = true;
                    return;
                }
                year = cbbNam.Text;
                month = cbbThang.Text;
                int sotang = 0;
                bool ktsd = false;
                Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[temp.Length];
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt = temp[i].NgayKetChuyen.Year.ToString();
                    string thangkt = temp[i].NgayKetChuyen.Month.ToString();

                    if (namkt == year && thangkt == month && temp[i].LoaiDoiTuong == loaiDoiTuong)
                    {
                        if (temp[i].TrangThai == false)
                        {
                            lbTrangThai.Text = "Kỳ này chưa đóng";
                            lbTrangThai.ForeColor = Color.Black;
                            btnKetChuyen.Enabled = true;
                        }
                        else
                        {
                            lbTrangThai.Text = "Kỳ này đã khóa";
                            lbTrangThai.ForeColor = Color.Red;
                            btnKetChuyen.Enabled = false;
                        }
                        ma = temp[i].MaSoDuCongNo;
                        sq[sotang] = temp[i];
                        sotang++;
                        ktsd = true;
                    }

                }
                if (ktsd == false)
                {
                    lbTrangThai.Text = "Kỳ này chưa đóng";
                    lbTrangThai.ForeColor = Color.Black;
                    btnKetChuyen.Enabled = true;
                }
                if (sotang != 0)
                {
                    hienthi = new Entities.SoDuCongNo[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi[i] = sq[i];
                    }
                }
                else
                    hienthi = null;
            }
            catch
            {
            }
            finally
            {

            }
            return;
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event
        private void frmKetChuyen_Load(object sender, EventArgs e)
        {
            lblTieuDe.Text = "Kết Chuyển Số Dư Công Nợ Kỳ " + date.Month + "/" + date.Year;
            lbChuY.Text = "Hãy chắc chắn hôm nay " + date.Day + "/" + date.Month + "/" + date.Year + "\nlà ngày giao dịch cuối cùng trong kỳ";
            this.FormClosing += new FormClosingEventHandler(frmKetChuyen_FormClosing);
            btnKetChuyen.Click += new EventHandler(btnKetChuyen_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            cbbDoiTuong.SelectedIndexChanged += new EventHandler(cbb_SelectedIndexChanged);
            cbbNam.SelectedIndexChanged += new EventHandler(cbb_SelectedIndexChanged);
            cbbThang.SelectedIndexChanged += new EventHandler(cbb_SelectedIndexChanged);
        }

        void cbb_SelectedIndexChanged(object sender, EventArgs e)
        {
            year = cbbNam.Text;
            month = cbbThang.Text;
            lblTieuDe.Text = "Kết Chuyển Số Dư Công Nợ Kỳ " + cbbThang.Text + "/" + cbbNam.Text;
            Select();
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnKetChuyen_Click(object sender, EventArgs e)
        {
            KetChuyen();
        }

        void frmKetChuyen_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion